package com.hxc.shop.controller;

import com.hxc.shop.bean.ShopImage;
import com.hxc.shop.common.ResultCode;
import com.hxc.shop.common.ResultVO;
import com.hxc.shop.service.FileService;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ViewResource;
import com.sun.jersey.api.client.WebResource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import java.nio.charset.StandardCharsets;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/file")
@CrossOrigin
@Api(tags = {"文件处理"})
public class FileController {
  @Resource
  private FileService fileService;

  @ApiOperation("上传本地文件")
  @PostMapping("/loadLocalFile")
  @Transactional(rollbackFor = Exception.class)
  public ResultVO loadLocalFile(@RequestParam("file")MultipartFile file,@RequestParam("path")String path) throws Exception {
    return fileService.loadLocalFile(file,path);
  }

//  @ApiOperation("ftp上传文件到服务器")
//  @PostMapping("/FtpUpload")
//  @Transactional(rollbackFor = Exception.class)
//  public ResultVO FtpUpload(@RequestParam("file")MultipartFile file,@RequestParam("path")String path) throws Exception {
//    return fileService.FtpUpload(file,path);
//  }
//
//
//  @ApiOperation("ftp读取图片")
//  @GetMapping("/FtpReadImageList")
//  @ApiImplicitParams({
//          @ApiImplicitParam(name = "path",value = "读取文件的路径-可为空",required = false,paramType = "String")
//  })
//  @Transactional(rollbackFor = Exception.class)
//  public ResultVO FtpReadImageList(@RequestParam("path")String path) throws Exception {
//    return fileService.FtpReadImageList(path);
//  }

    @ApiOperation("原文件名(不能中文)-跨域上传图片到服务器-")
    @PostMapping("/uploadImageToServerOriginalName")
    @ResponseBody
    public ResultVO uploadImageToServerOriginalName(@RequestParam("file")MultipartFile file){
      return fileService.uploadImageToServer(file);
    }

  @ApiOperation("随机文件名(可中文)跨域上传图片到服务器")
  @PostMapping("/uploadImageToServerRandName")
  @ResponseBody
  public ResultVO uploadImageToServerRandName(@RequestParam("file")MultipartFile file){
    return fileService.uploadImageToServerV2(file);
  }

    @ApiOperation("通过图片路径删除服务器图片")
    @GetMapping("/deleteImageFromServer")
      @ApiImplicitParams({
          @ApiImplicitParam(name = "path",value = "服务器文件位置",required = true,paramType = "String")
  })
    @ResponseBody
    public ResultVO deleteImageFromServer(String path){
        return fileService.deleteImageFromServer(path);
    }

  @ApiOperation("获取所有服务器图片")
  @GetMapping("/getAllImageFromServer")
  @ResponseBody
  public ResultVO getAllImageFromServer(){
    return fileService.getAllImageFromServer();
  }

  @ApiOperation("根据id获取图片信息")
  @GetMapping("getImageByteById")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "id",value = "编号",required = true,paramType = "Integer")
  })
  @ResponseBody
  public ResultVO getImageByteById(@RequestParam("id")Integer id) {
    return fileService.getImageByteById(id);
  }

  @ApiOperation("通过id直接获取图片文件")
  @GetMapping("getImageFileById")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "id",value = "编号",required = true,paramType = "Integer")
  })
  @ResponseBody
  public ResponseEntity<byte[]> getImageFileById(@RequestParam("id")Integer id) {
    return fileService.getImageFileById(id);
  }

  @ApiOperation("上传文件为字节流")
  @PostMapping("uploadFileToBytes")
  @ResponseBody
  public ResultVO uploadFileToBytes(@RequestParam("file") MultipartFile file){
    return fileService.uploadFileToBytes(file);
  }
}
